package com.lkl.service.impl;

import com.lkl.entity.BaseResp;
import com.lkl.entity.TgGoods;
import com.lkl.dao.TgGoodsDao;
import com.lkl.service.TgGoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TgGoods)表服务实现类
 *
 * @author makejava
 * @since 2022-04-08 10:33:06
 */
@Service("tgGoodsService")
public class TgGoodsServiceImpl implements TgGoodsService {
    @Resource
    private TgGoodsDao tgGoodsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BaseResp queryById(Integer id) {

        TgGoods tgGoods = this.tgGoodsDao.queryById(id);

        if (tgGoods == null) {
            return new BaseResp(0, "查询失败！", null, null);

        }
        return new BaseResp(1, "查询成功！", null, tgGoods);

    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<TgGoods> queryAllByLimit(int offset, int limit) {
        return this.tgGoodsDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param tgGoods 实例对象
     * @return 实例对象
     */
    @Override
    public TgGoods insert(TgGoods tgGoods) {
        this.tgGoodsDao.insert(tgGoods);
        return tgGoods;
    }

    /**
     * 修改数据
     *
     * @param tgGoods 实例对象
     * @return 实例对象
     */
    @Override
    public BaseResp update(TgGoods tgGoods) {
        this.tgGoodsDao.update(tgGoods);
        return this.queryById(tgGoods.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tgGoodsDao.deleteById(id) > 0;
    }

    @Override
    public BaseResp findAll() {

        List<TgGoods> list = tgGoodsDao.findAll();

        if (list.size() <= 0) {
            return new BaseResp(0, "查询失败！", null, null);

        }
        return new BaseResp(1, "查询成功！", null, list);


    }
}